/**
 * 后台通用js
 * @authors yxz (1085388169@qq.com)
 * @date    2015-05-13 17:57:05
 * @version $Id$
 */

var admin = {


    /**
     * 修复布局问题
     * @return {[type]} [description]
     */
    fixLayout: function() {
        //Get window height and the wrapper height
        var neg = $('.main-header').outerHeight() + $('.main-footer').outerHeight();
        var windowHeight = $(window).height();
        var sidebarHeight = $(".sidebar").height();

        $(".content-wrapper").css('min-height', windowHeight - $('.main-footer').outerHeight());
    },

    /**
     * treeview
     * @param  {[String]} menu [元素：css选择器]
     * @Usage: admin.treeview('.sidebar');
     */
    treeView: function(menu) {
        var _this = this;

        $("li a", $(menu)).on('click', function(e) {

            var $this = $(this);
            var checkElement = $this.next();

            //Check if the next element is a menu and is visible
            if ((checkElement.is('.treeview-menu')) && (checkElement.is(':visible'))) {
                //Close the menu
                checkElement.slideUp('normal', function() {
                    checkElement.removeClass('menu-open');
                    //Fix the layout in case the sidebar stretches over the height of the window
                    //_this.layout.fix();
                });
                checkElement.parent("li").removeClass("active");
                console.log(1);
            }
            //If the menu is not visible
            else if ((checkElement.is('.treeview-menu')) && (!checkElement.is(':visible'))) {
                //Get the parent menu
                var parent = $this.parents('ul').first();
                //Close all open menus within the parent
                var ul = parent.find('ul:visible').slideUp('normal');
                //Remove the menu-open class from the parent
                ul.removeClass('menu-open');
                //Get the parent li
                var parent_li = $this.parent("li");

                //Open the target menu and add the menu-open class
                checkElement.slideDown('normal', function() {
                    //Add the class active to the parent li
                    checkElement.addClass('menu-open');
                    parent.find('li.active').removeClass('active');
                    parent_li.addClass('active');

                    console.log(2);
                    //Fix the layout in case the sidebar stretches over the height of the window
                    // _this.layout.fix();
                });
            }
            //if this isn't a link, prevent the page from being redirected
            if (checkElement.is('.treeview-menu')) {
                e.preventDefault();
            }
        });
    },



    /**
     * js本地图片预览
     * 对于 Chrome、Firefox、IE10+ 使用 FileReader 来实现。
     * 对于 IE6~9 使用滤镜 filter:progid:DXImageTransform.Microsoft.AlphaImageLoader 来实现。
     * @param {[type]} fileObj      [description]
     * @param {[type]} imgPreviewId [description]
     * @param {[type]} divPreviewId [description]
     */
    previewImage: function preview(file, warp) {
        if (file.files && file.files[0]) {
            var reader = new FileReader();
            reader.onload = function(event) {
                warp.innerHTML = '<img class="avatar" src="' + this.result + '" />';
                console.log(this.result);
            };
            reader.readAsDataURL(file.files[0]);
        } else {
            warp.innerHTML = '<div class="avatar" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + file.value + '\'"></div>';
            console.log(file.value);
        }
    },

    /**
     * 初始化
     * 
     */
    init: function() {
        var _this = this;

        this.fixLayout();
        this.treeView('.sidebar');

        // 浏览器大小改变
        $(window, ".wrapper").resize(function() {
            _this.fixLayout();
        });

    }
};
